package net.soporte.action;

import java.io.FileInputStream;
import java.io.OutputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.jasperreports.engine.JasperCompileManager;
import net.sf.jasperreports.engine.JasperExportManager;
import net.sf.jasperreports.engine.JasperFillManager;
import net.sf.jasperreports.engine.JasperPrint;
import net.sf.jasperreports.engine.JasperReport;
import net.sf.jasperreports.engine.JasperRunManager;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

public class RunReportAction extends Action {

	private String driver ="oracle.jdbc.driver.OracleDriver";
	private String url ="jdbc:oracle:thin:@localhost:1521:XE";
	private String userName="TSC";
	private String password="TSC";
	
	public ActionForward execute(ActionMapping mapping,
			ActionForm form,
			HttpServletRequest request,
			HttpServletResponse response)throws Exception
	{ 
		
		Class.forName(driver);
		Connection cn = DriverManager.getConnection(url,userName,password);
		
		System.out.println(getServlet().getServletContext()
				.getRealPath("/WEB-INF/reports/fallo.jasper"));
		
		Map <String,Object> map= new HashMap<String,Object>();
		
		map.put("desde", "0");
		map.put("hasta", "1");
		
//		FileInputStream fis= new FileInputStream(getServlet().getServletContext()
//				.getRealPath("/WEB-INF/reports/fallo.jasper"));
		
		response.setContentType("application/pdf");
		
		OutputStream out = response.getOutputStream();
		
		//System.out.println(getServlet().getServletContext()
		//		.getRealPath("/WEB-INF/reports/fallo.jasper"));
		
		//JasperRunManager.runReportToPdfStream(fis, out,map,cn);
		
		//JasperRunManager.runReportToPdfStream( 
		//		getClass().getClassLoader().getResourceAsStream( "/WEB-INF/reports/fallo.jasper"), 
		//		response.getOutputStream(), map, cn );	
		
		
		//JasperReport report = JasperCompileManager.compileReport(getClass().getClassLoader().getResourceAsStream(getServlet().getServletContext()
		//		.getRealPath("/WEB-INF/reports/fallo.jrxml")));
		
		JasperReport report = JasperCompileManager.compileReport(getServlet().getServletContext()
				.getRealPath("/WEB-INF/reports/fallo2.jrxml"));
		JasperPrint print = JasperFillManager.fillReport(report, map, cn);      
		JasperExportManager.exportReportToPdfStream(print, out);
		
		
		
		//JasperRunManager.runreporttopdfs
		//fis.close();
		out.flush();
		
		return (ActionForward)null;
	}
	
}
